package fr.irit.etracetool.etrace.util;

import fr.irit.etracetool.etrace.*;
import org.eclipse.emf.common.util.EList;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import astjava.*;

public class EtraceToDot
{
  protected static String nl;
  public static synchronized EtraceToDot create(String lineSeparator)
  {
    nl = lineSeparator;
    EtraceToDot result = new EtraceToDot();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "\t";
  protected final String TEXT_2 = NL;
  protected final String TEXT_3 = NL + "\t\t\t";
  protected final String TEXT_4 = "[label=\"";
  protected final String TEXT_5 = "\"];" + NL + "\t\t\t";
  protected final String TEXT_6 = "[label=\"";
  protected final String TEXT_7 = "\"];" + NL + "\t\t\t";
  protected final String TEXT_8 = "->";
  protected final String TEXT_9 = "[label=\"";
  protected final String TEXT_10 = "\", color=orange];" + NL + "\t\t";
  protected final String TEXT_11 = NL + "\t\t\tsubgraph cluster_";
  protected final String TEXT_12 = " {" + NL + "\t\t\t\tlabel = \"";
  protected final String TEXT_13 = "\";" + NL + "\t\t\t\t";
  protected final String TEXT_14 = NL + "\t\t\t}" + NL + "\t\t";
  protected final String TEXT_15 = NL + "\t\t";
  protected final String TEXT_16 = "[label=\"";
  protected final String TEXT_17 = "\", shape=house, style=filled];" + NL + "\t\t";
  protected final String TEXT_18 = "[label=\"";
  protected final String TEXT_19 = "\", style=filled, color=yellow];" + NL + "\t\t";
  protected final String TEXT_20 = "->";
  protected final String TEXT_21 = ";" + NL + "\t";
  protected final String TEXT_22 = NL + "\t\t";
  protected final String TEXT_23 = "[label=\"";
  protected final String TEXT_24 = "\"];" + NL + "\t\t";
  protected final String TEXT_25 = "[label=\"";
  protected final String TEXT_26 = "\"];" + NL + "\t\t";
  protected final String TEXT_27 = "->";
  protected final String TEXT_28 = "[label=\"";
  protected final String TEXT_29 = "\", style=dashed];" + NL + "\t";
  protected final String TEXT_30 = NL + "\t\t";
  protected final String TEXT_31 = NL + "\t\t\t";
  protected final String TEXT_32 = "[label=\"";
  protected final String TEXT_33 = "\"];" + NL + "\t\t\t";
  protected final String TEXT_34 = "[label=\"";
  protected final String TEXT_35 = "\"];" + NL + "\t\t\t";
  protected final String TEXT_36 = "->";
  protected final String TEXT_37 = "[color=green];" + NL + "\t\t";
  protected final String TEXT_38 = NL + "\t";
  protected final String TEXT_39 = NL + "\t\t";
  protected final String TEXT_40 = "[label=\"";
  protected final String TEXT_41 = "\"];" + NL + "\t\t";
  protected final String TEXT_42 = "[label=TRASH, shape=box, style=filled];" + NL + "\t\t";
  protected final String TEXT_43 = "->";
  protected final String TEXT_44 = "[color=red];" + NL + "\t";
  protected final String TEXT_45 = NL + NL + "digraph G {" + NL + "\tlabel = \"";
  protected final String TEXT_46 = "\";" + NL + "\t";
  protected final String TEXT_47 = NL + "}";
  protected final String TEXT_48 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    ETrace trace = (ETrace) argument; 
    stringBuffer.append(TEXT_2);
    
class Util {
	int id=1;
	public void firstCategory(AbstractLink l) {
		id++;
		if (l instanceof Link) {
    stringBuffer.append(TEXT_3);
    stringBuffer.append(l.getSource().hashCode());
    stringBuffer.append(TEXT_4);
    stringBuffer.append(generateStringName(l.getSource()));
    stringBuffer.append(TEXT_5);
    stringBuffer.append(l.getTarget().hashCode());
    stringBuffer.append(TEXT_6);
    stringBuffer.append(generateStringName(l.getTarget()));
    stringBuffer.append(TEXT_7);
    stringBuffer.append(l.getSource().hashCode());
    stringBuffer.append(TEXT_8);
    stringBuffer.append(l.getTarget().hashCode());
    stringBuffer.append(TEXT_9);
    stringBuffer.append(l.getType().getName());
    stringBuffer.append(TEXT_10);
    }
		if (l instanceof CompositeLink) {
    stringBuffer.append(TEXT_11);
    stringBuffer.append(l.hashCode());
    stringBuffer.append(TEXT_12);
    stringBuffer.append(l.getType().getName());
    stringBuffer.append(TEXT_13);
    EList<AbstractLink> children = ((CompositeLink)l).getChildren();
				Util u=new Util();
				for (int i=0; i<children.size(); i++) {
					if(children.get(i).getType().getSuperType().getName() == "Fonctionnal Method") {
						u.firstCategory(children.get(i));
					}
					if(children.get(i).getType().getSuperType().getName() == "Factory method") {
						u.secondCategory(children.get(i),id);
					}
					if(children.get(i).getType().getSuperType().getName() == "Setter Method") {
						u.thirdCategory(children.get(i),id);
					}
					if(children.get(i).getType().getSuperType().getName() == "Add Feature") {
						u.fourthCategory(children.get(i));
					}
					if(children.get(i).getType().getSuperType().getName() == "Remove Feature") {
						u.fifthCategory(children.get(i), id);
					}
				}
				
    stringBuffer.append(TEXT_14);
    }
	}
	
	public void secondCategory(AbstractLink l, int id) {
    stringBuffer.append(TEXT_15);
    stringBuffer.append(l.getSource().hashCode()+id);
    stringBuffer.append(TEXT_16);
    stringBuffer.append(l.getSource().eClass().getName());
    stringBuffer.append(TEXT_17);
    stringBuffer.append(l.getTarget().hashCode());
    stringBuffer.append(TEXT_18);
    stringBuffer.append(generateStringName(l.getTarget()));
    stringBuffer.append(TEXT_19);
    stringBuffer.append(l.getSource().hashCode()+id);
    stringBuffer.append(TEXT_20);
    stringBuffer.append(l.getTarget().hashCode());
    stringBuffer.append(TEXT_21);
    }
	public void thirdCategory(AbstractLink l,int id) {
    stringBuffer.append(TEXT_22);
    stringBuffer.append(l.getSource().hashCode());
    stringBuffer.append(TEXT_23);
    stringBuffer.append(generateStringName(l.getSource()));
    stringBuffer.append(TEXT_24);
    stringBuffer.append(l.getTarget().hashCode());
    stringBuffer.append(TEXT_25);
    stringBuffer.append(generateStringName(l.getTarget()));
    stringBuffer.append(TEXT_26);
    stringBuffer.append(l.getSource().hashCode());
    stringBuffer.append(TEXT_27);
    stringBuffer.append(l.getTarget().hashCode());
    stringBuffer.append(TEXT_28);
    stringBuffer.append(l.getType().getName());
    stringBuffer.append(TEXT_29);
    
	}
	public void fourthCategory(AbstractLink l) {
    stringBuffer.append(TEXT_30);
    if (l.getTarget() != null) {
    stringBuffer.append(TEXT_31);
    stringBuffer.append(l.getSource().hashCode());
    stringBuffer.append(TEXT_32);
    stringBuffer.append(generateStringName(l.getSource()));
    stringBuffer.append(TEXT_33);
    stringBuffer.append(l.getTarget().hashCode());
    stringBuffer.append(TEXT_34);
    stringBuffer.append(generateStringName(l.getTarget()));
    stringBuffer.append(TEXT_35);
    stringBuffer.append(l.getSource().hashCode());
    stringBuffer.append(TEXT_36);
    stringBuffer.append(l.getTarget().hashCode());
    stringBuffer.append(TEXT_37);
    }
    stringBuffer.append(TEXT_38);
    
	}
	public void fifthCategory(AbstractLink l, int id) {
    stringBuffer.append(TEXT_39);
    stringBuffer.append(l.getSource().hashCode());
    stringBuffer.append(TEXT_40);
    stringBuffer.append(generateStringName(l.getSource()));
    stringBuffer.append(TEXT_41);
    stringBuffer.append(id);
    stringBuffer.append(TEXT_42);
    stringBuffer.append(l.getSource().hashCode());
    stringBuffer.append(TEXT_43);
    stringBuffer.append(id);
    stringBuffer.append(TEXT_44);
    
	}
	
	public String generateStringName(EObject element) {
		if (element instanceof ENamedElement) {
			return ((ENamedElement) element).getName();
		}
		if (element instanceof org.eclipse.uml2.uml.NamedElement) {
			return ((org.eclipse.uml2.uml.NamedElement) element).getName();
		}
		if (element instanceof com.googlecode.erca.NamedElement) {
			return ((com.googlecode.erca.NamedElement) element).getName();
		}
		if (element instanceof astjava.TypeAccess) {
			return "TypeAccess -> " + ((astjava.TypeAccess) element).getType().getName();
		}
		if (element instanceof astjava.Modifier) {
			return "Modifier st : " + ((astjava.Modifier) element).isStatic() + "\\n V : " + ((astjava.Modifier) element).getVisibility().getName();
		}
		if (element instanceof astjava.NamedElement) {
			return ((astjava.NamedElement) element).getName() + " : " + element.eClass().getName();
		}
		return element.eClass().getName();
	}
}
	
    stringBuffer.append(TEXT_45);
    stringBuffer.append(trace.getName());
    stringBuffer.append(TEXT_46);
    
	EList<AbstractLink> children = trace.getChildren();
	Util u=new Util();
	for (int i=0; i<children.size(); i++) {
		if(children.get(i).getType().getSuperType().getName() == "Fonctionnal Method") {
			u.firstCategory(children.get(i));
		}
		if(children.get(i).getType().getSuperType().getName() == "Factory method") {
			u.secondCategory(children.get(i),u.id);
		}
		if(children.get(i).getType().getSuperType().getName() == "Setter Method") {
			u.thirdCategory(children.get(i),u.id);
		}
		if(children.get(i).getType().getSuperType().getName() == "Add Feature") {
			u.fourthCategory(children.get(i));
		}
		if(children.get(i).getType().getSuperType().getName() == "Remove Feature") {
			u.fifthCategory(children.get(i), u.id);
		}
	}
	
    stringBuffer.append(TEXT_47);
    stringBuffer.append(TEXT_48);
    return stringBuffer.toString();
  }
}
